import HeaderComponent from "@/components/HeaderComponent/HeaderComponent";

import { Input, Toast } from "antd-mobile";
import { useState } from "react";
import { useTranslation } from "react-i18next";

import { changePayPwd } from "@/api/module/user";
import { CenterPopupComponent } from "@/components/CenterPopupComponent/CenterPopupComponent";

const FormComponent = () => {
  const { t } = useTranslation();
  const [password, setPassword] = useState("");
  const [confirmPassword, setconfirmPassword] = useState("");
  const [googleCode, setgoogleCode] = useState("");

  const confirm = async () => {
    try {
      Toast.show({
        icon: "loading",
      });
      const res = await changePayPwd({
        google_code: googleCode,
        new_password: password,
        confirm_password: confirmPassword,
      });
      Toast.clear();
      Toast.show({
        content: res.msg,
      });
      res.code == 0 &&
        (() => {
          setPassword("");
          setconfirmPassword("");
          setgoogleCode("");
        })();
    } catch (error) {
      Toast.clear();
    }
  };

  const [visible, setVisible] = useState(false);
  const showModal = () => {
    if (
      !/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/.test(
        password
      )
    ) {
      Toast.show({
        content: t("passwordFormatError"),
      });
      return;
    }
    // || !googleCode
    if (!password || !confirmPassword) {
      Toast.show({
        content: t("enterCompleteInformation"),
      });
      return;
    }
    if (password !== confirmPassword) {
      Toast.show({
        content: t("passwordNotMatch"),
      });
      return;
    }
    setVisible(true);
  };
  return (
    <div
      className="mt-4"
      style={{
        fontSize: "95%",
      }}
    >
      <div
        className="tips text-font-2 text-center mb-8
      "
      >
        {t("pleaseEnter")}
      </div>
      <div className="USDT mt-4">
        <div>{t("password")}</div>
        <Input
          type="password"
          style={{
            "--font-size": "14px",
            "--color": "#fff",
          }}
          className="bg-bg-1 py-4 mt-3 px-3 rounded-lg "
          placeholder={`${t("enter")}`}
          value={password}
          onChange={(val) => {
            setPassword(val);
          }}
        />
        <div className="mt-2 opacity-50 text-xs">
          <span>{t("passwordRule")}(@$!%*#?&)</span>
        </div>
      </div>
      <div className="confirmPassword mt-4">
        <div>{t("confirmPassword")}</div>
        <Input
          type="password"
          style={{
            "--font-size": "14px",
            "--color": "#fff",
          }}
          className="bg-bg-1 py-4 mt-3 px-3 rounded-lg "
          placeholder={`${t("enter")}`}
          value={confirmPassword}
          onChange={(val) => {
            setconfirmPassword(val);
          }}
        />
      </div>
      {/* <div className="password mt-4">
        <div>{t('googleCode')}</div>
        <Input
          style={{
            '--font-size': '14px',
            '--color': '#fff',
          }}
          type="password"
          className="bg-bg-1 py-4 mt-3 px-3 rounded-lg "
          placeholder={`${t('enter')}`}
          value={googleCode}
          onChange={(val) => {
            setgoogleCode(val);
          }}
        />
      </div> */}
      <div
        onClick={() => showModal()}
        className="mt-6 bg-bg-16 py-4 rounded-md text-center text-base"
      >
        {t("confirm")}
      </div>
      <CenterPopupComponent
        visible={visible}
        setVisible={setVisible}
        title="updatePassword"
        onClick={() => confirm()}
      />
    </div>
  );
};
export default function UpdatePassword() {
  return (
    <div className="text-white-1 px-4">
      <HeaderComponent title="updatePassword" />
      <FormComponent />
    </div>
  );
}
